package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.mapper.PrivateMessageMapper;
import cn.wolfcode.trip.base.query.PrivateMessageQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author saber
 * @date 2018/11/14  21:20
 * @description:
 */

@Service
public class PrivateMessageServiceImpl implements IPrivateMessageService {

    @Autowired
    private PrivateMessageMapper privateMessageMapper;

    /**
     * 保存私信
     *
     * @param message
     */
    @Override
    public void saveMessage(String message) {
        PrivateMessage privateMessage = JSONUtil.toJSON(message);
        Date date = new Date();
        privateMessage.setSendTime(date);
        privateMessageMapper.insert(privateMessage);
        Long temp = null;
        temp = privateMessage.getFriendId();
        privateMessage.setFriendId(privateMessage.getUserId());
        privateMessage.setUserId(temp);
        privateMessage.setStatus(PrivateMessage.STATUS_UNREAD);
        privateMessageMapper.insert(privateMessage);
    }

    @Override
    public PageInfo getMessagesByUserIdAndFriendId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List list = privateMessageMapper.selectMessagesByUserIdAndFriendId(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Map> getFriendIdAndUnReadCount(QueryObject qo) {

        List<Map> list = privateMessageMapper.getFriendIdAndUnReadCount(qo);
        System.out.println("哈哈" + list);
        return list;
    }

    @Override
    public void updateStatus(Long userId, Long friendId, Integer status, String sendTime) {
        privateMessageMapper.updateStatus(userId, friendId, status, sendTime);
    }

    @Override
    public Integer getUnRead(Long userId, Integer status) {
        Integer count = privateMessageMapper.getUnRead(userId, status);
        return count;
    }
}
